package cn.zjyj.module.zjyj.controller.admin.riskidentifytargetcontent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.riskidentifytargetcontent.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifytargetcontent.RiskIdentifyTargetContentDO;
import cn.zjyj.module.zjyj.service.riskidentifytargetcontent.RiskIdentifyTargetContentService;

@Tag(name = "管理后台 - 风险辨识内容")
@RestController
@RequestMapping("/zjyj/risk-identify-target-content")
@Validated
public class RiskIdentifyTargetContentController {

    @Resource
    private RiskIdentifyTargetContentService riskIdentifyTargetContentService;

    @PostMapping("/create")
    @Operation(summary = "创建风险辨识内容")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:create')")
    public CommonResult<Long> createRiskIdentifyTargetContent(@Valid @RequestBody RiskIdentifyTargetContentSaveReqVO createReqVO) {
        return success(riskIdentifyTargetContentService.createRiskIdentifyTargetContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险辨识内容")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:update')")
    public CommonResult<Boolean> updateRiskIdentifyTargetContent(@Valid @RequestBody RiskIdentifyTargetContentSaveReqVO updateReqVO) {
        riskIdentifyTargetContentService.updateRiskIdentifyTargetContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险辨识内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:delete')")
    public CommonResult<Boolean> deleteRiskIdentifyTargetContent(@RequestParam("id") Long id) {
        riskIdentifyTargetContentService.deleteRiskIdentifyTargetContent(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险辨识内容")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:delete')")
    public CommonResult<Boolean> deleteRiskIdentifyTargetContentList(@RequestParam("ids") List<Long> ids) {
        riskIdentifyTargetContentService.deleteRiskIdentifyTargetContentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险辨识内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:query')")
    public CommonResult<RiskIdentifyTargetContentRespVO> getRiskIdentifyTargetContent(@RequestParam("id") Long id) {
        RiskIdentifyTargetContentDO riskIdentifyTargetContent = riskIdentifyTargetContentService.getRiskIdentifyTargetContent(id);
        return success(BeanUtils.toBean(riskIdentifyTargetContent, RiskIdentifyTargetContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险辨识内容分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:query')")
    public CommonResult<PageResult<RiskIdentifyTargetContentRespVO>> getRiskIdentifyTargetContentPage(@Valid RiskIdentifyTargetContentPageReqVO pageReqVO) {
        PageResult<RiskIdentifyTargetContentDO> pageResult = riskIdentifyTargetContentService.getRiskIdentifyTargetContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskIdentifyTargetContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险辨识内容 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-target-content:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskIdentifyTargetContentExcel(@Valid RiskIdentifyTargetContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskIdentifyTargetContentDO> list = riskIdentifyTargetContentService.getRiskIdentifyTargetContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险辨识内容.xls", "数据", RiskIdentifyTargetContentRespVO.class,
                        BeanUtils.toBean(list, RiskIdentifyTargetContentRespVO.class));
    }

}